<?php
declare(strict_types = 1);

namespace plugin\banmichaoqiangbizhi\api;

use app\banmi\common\Basehome;
use think\facade\Db;
 
 
use think\facade\Log;

class Sign extends Basehome
{
   
     
    public function plugin_cn()
    {
        return 'banmichaoqiangbizhi';
    }
       
    public function my_signin()
    {
        $uid = $this->uid;
        // 查询今日是否已签到
        $today_time = strtotime(date('Y-m-d'));
        $name_log='plugin_banmi_banmichaoqiangbizhi_users_sign_log';
        $log = Db::name($name_log)
            ->where('uid', $uid)
            ->where('time', '>=', $today_time)
            ->count();
        $data['log']=$log;
        $user_find=  Db::name('plugin_banmi_banmichaoqiangbizhi_users_money')->where('uid', $uid)->find();
        if (!$user_find) {
            $user_find['money']=0;
            $user_find['integral']=0;
        }
        
        $data['user_find']=$user_find;
        ajaxmsg('ok', 200, $data);
    }
       
    //判断是否下载过
    public function set_download()
    {
        $uid =$this->uid;
        $shop_id = input('request.shop_id');
        if (empty($shop_id)) {
            ajaxmsg('非法操作', 0);
        }
        $map['uid']=$uid;
        $map['shop_id']=$shop_id;
        $is_find =  Db::name('plugin_banmi_'.$this->plugin_cn().'_download')->where($map)->find();
        if ($is_find) {
            ajaxmsg('ok', 200);
        }
        //扣除积分
        $set_json = plugin_config(0);
         
        $download_num=$set_json['download_num'];
        $name='plugin_banmi_banmichaoqiangbizhi_users_money';
        $users_money=  Db::name($name)->where('uid', $uid)->lock(true)->find();
        if (!$users_money) {
            ajaxmsg('积分不足', 0);
        }
        if ($download_num>$users_money['integral']) {
            ajaxmsg('积分不足', 0);
        }
         
         
        $add_=  $this->_add($uid, $text='下载图片', $state=0, $number=$download_num, $type=1);
        $result =  Db::name('plugin_banmi_'.$this->plugin_cn().'_download')->insert([
            'uid' => $uid,
            'shop_id' => $shop_id ,
            'creation_time' => time() ,
            ]);
          
        ajaxmsg('ok', 200);
    }
       
       
   
    //签到
    public function signin()
    {
        $uid = $this->uid;
        $controller=  $this->plugin_cn();
        // 查询今日是否已签到
        $today_time = strtotime(date('Y-m-d'));
    
        $log = Db::name('plugin_banmi_banmichaoqiangbizhi_users_sign_log')
            ->where('uid', $uid)
            ->where('type', 0)
            ->where('time', '>=', $today_time)
            ->count();
        if ($log > 0) {
            ajaxmsg('今天已经签到过啦', 0);
        }
 
      
         
        $set_json = plugin_config(0);
        $add=  $this->_add($uid, $text='每日签到', $state=1, $number=$set_json['sing_num'], $type=0);
       
        if ($add==200) {
            ajaxmsg('签到成功', 200);
        }
        ajaxmsg('签到失败', 0);
    }
       
    // $state 0扣除 1增加
    // $text 操作原因
    // $uid用户uid
    //$number 积分
    // $type =0 签到 1下载图片
    public function _add($uid, $text, $state, $number, $type)
    {
        $name='plugin_banmi_banmichaoqiangbizhi_users_money';
        $name_log='plugin_banmi_banmichaoqiangbizhi_users_sign_log';
        Db::startTrans();
        try {
            $users_money=  Db::name($name)->where('uid', $uid)->lock(true)->find();
            if (!$users_money) { //用户积分表不存在 新增
                Db::name($name)->insert(['integral' =>$number, 'uid' => $uid ]);
            } else {//更新
                if ($state == 0) {
                    $integral =$users_money['integral'] - $number;
                } else {
                    $integral =$users_money['integral'] + $number;
                }
               
                Db::name($name)
                  ->where('uid', $uid)
                  ->data(['integral' => $integral ])
                  ->update();
            }
            //余额日志
            $users_money_log=  Db::name($name_log)->where('uid', $uid)->order('id desc')->find();
            $statistics_number=$number;
            if ($users_money_log) {//日志存在
                //剩余余额
                if ($state == 0) {
                    $statistics_number = $users_money_log['statistics_number'] - $number;
                } else {
                    $statistics_number = $users_money_log['statistics_number'] + $number;
                }
            }
            Db::name($name_log)->insert([
                    'number' =>$number,
                    'statistics_number' =>$statistics_number,
                    'text' => $text ,
                    'uid' => $uid ,
                    'time'=>time() ,
                    'ip'=>request()->ip(),
                    'type'=>$type
                     ]);
               
               
            Db::commit();
            return 200;
        } catch (\Exception $ex) {
            Db::rollback();
            return 1;
        }
    }
}
